
package {

    import flash.display.Sprite;
    import flash.display.LineScaleMode;
    import flash.display.CapsStyle;
    import flash.display.JointStyle;

    import flash.events.Event;
    import flash.events.MouseEvent;

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;

    public class Button extends Sprite {

	private var __borderColor : int = 0xcccccc;
	private var __fillColor : int = 0xcc0000;
	private var __width : int = 120;
	private var __height : int = 40;
	private var __borderWidth : int = 5;
	
	private var labelField : TextField;
	private var container : Sprite;
	private var format : TextFormat = new TextFormat();

	public function Button(label:String,callback:Function) {

	    buttonMode = true;

	    labelField = new TextField();
	    labelField.text = label;

	    format.size = 24;
	    format.color = 0xcccccc;

	    labelField.setTextFormat(format); 
	    labelField.autoSize = TextFieldAutoSize.CENTER;
	    labelField.selectable = false;
	    labelField.mouseEnabled = false;

	    container = new Sprite;
	    container.addChild(labelField);
	    
	    centerText();

	    addChild(container);
	    centerContainer();

	    if (callback != null) {
		addEventListener(MouseEvent.MOUSE_DOWN,callback);
	    }

	    redraw();
	}

	public function centerText() : void {
	    labelField.x = -labelField.textWidth/2;
	    labelField.y = -labelField.textHeight/2;
	}

	public function redraw() : void {
	    drawBorder();
	}

	public function drawBorder() : void {
	    graphics.clear();
	    graphics.beginFill(__fillColor,100);
	    graphics.lineStyle(__borderWidth,__borderColor,1.0,
			       true,LineScaleMode.NORMAL,
			       CapsStyle.ROUND,JointStyle.ROUND);
	    graphics.moveTo(0,0);
	    graphics.lineTo(__width,0);
	    graphics.lineTo(__width,__height);
	    graphics.lineTo(0,__height);
	    graphics.lineTo(0,0);
	}

	public function centerContainer() : void {
	    container.x = __width/2;
	    container.y = __height/2;
	}

	public function set borderColor(newColor:int) : void {
	    __borderColor = newColor;
	    redraw();
	}

	public function get borderColor() : int {
	    return __borderColor;
	}

	public function set fillColor(newColor:int) : void {
	    __fillColor = newColor;
	    redraw();
	}

	public function get fillColor() : int {
	    return __fillColor;
	}

	public function set textColor(newColor:Object) : void {
	    format.color = newColor;
	    labelField.setTextFormat(format);
	}

	public function get textColor() : Object {
	    return format.color;
	}

	public function set buttonWidth(newWidth:int) : void {
	    if (newWidth > 0) {
		__width = newWidth;
		centerContainer();
		redraw();
	    }
	}

	public function get buttonWidth() : int {
	    return __width;
	}

	public function set buttonHeight(newHeight:int) : void {
	    if (newHeight > 0) {
		__height = newHeight;
		centerContainer();
		redraw();
	    }
	}
	
	public function get buttonHeight() : int {
	    return __height;
	}

	public function set textSize(newSize:int) : void {
	    if (newSize > 0) {
		format.size = newSize;
		labelField.setTextFormat(format); 
		centerText();
	    }
	}

	public function get textSize() : int {
	    return format.size as int;
	}

	public function set borderWidth(newWidth:int):void {
	    if (newWidth > 0) {
		__borderWidth = newWidth;
		redraw();
	    }
	}

	public function get borderWidth():int {
	    return __borderWidth;
	}
    }
}
